using System.Collections.Generic;
using System.Text;

namespace WineCellar.Web.Helpers
{
    public class ExtColumnModel
    {
        /// <summary>
        /// Sets the DataIndex, used internally by the grid
        /// </summary>
        /// <value>The index.</value>
        public string DataIndex { get; set; }

        /// <summary>
        /// Sets the header for the column
        /// </summary>
        /// <value>The width.</value>
        public string Header { get; set; }

        /// <summary>
        ///  Set the CSS text-align property of the column. Defaults to undefined
        /// </summary>
        public string Align { get; set; }


        public string JavaScriptString
        {
            get
            {
                var properties = new List<string>(2);

                if (!string.IsNullOrEmpty(DataIndex))
                {
                    properties.Add(string.Format("dataIndex:{0}", JsonEncoder.Enquote(DataIndex)));
                }

                if (!string.IsNullOrEmpty(Header))
                {
                    properties.Add(string.Format("header:{0}", JsonEncoder.Enquote(Header)));
                }
                if (!string.IsNullOrEmpty(Header))
                {
                    properties.Add(string.Format("align:{0}", JsonEncoder.Enquote(Align)));
                }

                var sb = new StringBuilder();
                sb.Append("{");

                for (int i = 0; i < properties.Count; i++)
                {
                    if (i == (properties.Count - 1))
                    {
                        sb.Append(properties[i]);
                    }
                    else
                    {
                        sb.Append(properties[i]).Append(",");
                    }
                }

                sb.Append("}");

                return sb.ToString();
            }
        }
    }
}